package com.example.administrator.newsdemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
ArrayList<News> newsArrayList;
ListView mLvNews;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        //初始化界面所需要的数据
        initView();
        //初始化界面
        iniEvent();
        //设置事件
    }

    private void iniEvent() {
        mLvNews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(MainActivity.this,DetailActivity.class);
                intent.putExtra("position",newsArrayList.get(position).getIcon());
                intent.putExtra("icon",newsArrayList.get(position).getIcon());
                intent.putExtra("type",newsArrayList.get(position).getType());
                intent.putExtra("title",newsArrayList.get(position).getTitle());
                intent.putExtra("comment",newsArrayList.get(position).getComment());


            }
        });
    }

    private void initView() {
      mLvNews = findViewById(R.id.lv_news);
      MyBaseAdapter adapter = new MyBaseAdapter();
      mLvNews.setAdapter(adapter);

    }

    private void initData() {
        String str =redNewsJson();
        newsArrayList = parseJSON(str);
    }

    private ArrayList<News> parseJSON(String str) {
        //TODO 要动态解析json 字符串str
        ArrayList<News> newsList = new ArrayList<>();

        try {
            JSONArray jsonArray = new JSONArray(str);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                News news = new News();
                news.setIcon(jsonObject.optString("icon"));
                news.setComment(jsonObject.optString("comment"));
                news.setTitle(jsonObject.optString("title"));
                news.setType(jsonObject.optString("type"));
                news.setContent(jsonObject.optString("content"));
                newsList.add(news);
        }
    }catch (JSONException e) {
            e.printStackTrace();

        }
        /*News news = new News();
        news.setIcon("a.jpg");
        news.setComment("34");
        news.setTitle("搜狗输入板");
        news.setType("1");
        news.setContent("收购输入法的简介");
        newsList.add(news);*/
        return newsList;
    }

    private String redNewsJson() {
        //TODO 等会儿从newsjson中读取，现在暂时用固定字符串临时代替
        String str = "";
        InputStream inputStream = getResources().openRawResource(R.raw.news);
        try {
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes);
            inputStream.close();
            str = new String(bytes);
            return str;

        } catch (IOException e) {

            e.printStackTrace();
        }

        return str;



        /*return "[\n" +
                "  {\n" +
                "    \"icon\":\"a.jpg\",\n" +
                "    \"title\":\"科技温暖世界\",\n" +
                "    \"content\":\"进入一个更有爱的领域\",\n" +
                "\t\"type\":\"1\",\n" +
                "\t\"comment\":\"69\"\n" +
                "  },\n" +
                "  {\n" +
                "\t\"icon\":\"b.jpg\",\n" +
                "\t\"title\":\"《神武》\",\n" +
                "\t\"content\":\"新美术资源盘点 视觉新体验\",\n" +
                "\t\"type\":\"2\",\n" +
                "\t\"comment\":\"35\"\n" +
                "  },\n" +
                "  {\n" +
                "\t\"icon\":\"c.jpg\",\n" +
                "\t\"title\":\"南北车正式公布合并\",\n" +
                "\t\"content\":\"南北车将于今日正式公布合并\",\n" +
                "\t\"type\":\"3\",\n" +
                "\t\"comment\":\"2\"\n" +
                "  },\n" +
                "  {\n" +
                "\t\"icon\":\"d.jpg\",\n" +
                "\t\"title\":\"萌呆了！汪星人抱玩偶酣睡\",\n" +
                "\t\"content\":\"汪星人抱玩偶酣睡,萌翻网友\",\n" +
                "\t\"type\":\"1\",\n" +
                "\t\"comment\":\"25\"\n" +
                "  },\n" +
                "  {\n" +
                "\t\"icon\":\"e.jpg\",\n" +
                "\t\"title\":\"风力发电进校园\",\n" +
                "\t\"content\":\"风力发电普进校园\",\n" +
                "\t\"type\":\"2\",\n" +
                "\t\"comment\":\"26\"\n" +
                "  },\n" +
                "  {\n" +
                "\t\"icon\":\"f.jpg\",\n" +
                "\t\"title\":\"地球一小时\",\n" +
                "\t\"content\":\"地球熄灯一小时\",\n" +
                "\t\"type\":\"1\",\n" +
                "\t\"comment\":\"23\"\n" +
                "  },\n" +
                "  {\n" +
                "\t\"icon\":\"g.jpg\",\n" +
                "\t\"title\":\"最美公路\",\n" +
                "\t\"content\":\"最美公路 难以想象\",\n" +
                "\t\"type\":\"1\",\n" +
                "\t\"comment\":\"23\"\n" +
                "  }\n" +
                "]\n";*/
    }
    class MyBaseAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return newsArrayList.size();
        }

        @Override
        public Object getItem(int position) {
            return newsArrayList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
           View view =  View.inflate(MainActivity.this,R.layout.list_item_layot,null);
            ImageView icon = view.findViewById(R.id.iv_icon);

            //TODO 需要动态设置图标

            String pic = newsArrayList.get(position).getIcon();

             int mipmap = getResources().getIdentifier(pic.substring(0,pic.indexOf("."))
                     , "mipmap"
                     ,getPackageName());


            icon.setImageResource(mipmap);
            TextView textView = view.findViewById(R.id.tv_title);
            textView.setText(newsArrayList.get(position).getTitle());
            return view;
        }
    }

    class News{

           private String icon;
           private String title;
           private String content;
           private String type;
           private String comment;

        public String getIcon() {
            return icon;
        }

        public void setIcon(String icon) {
            this.icon = icon;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getComment() {
            return comment;
        }

        public void setComment(String comment) {
            this.comment = comment;
        }
/*{
            "icon":"a.jpg",
                "title":"科技温暖世界",
                "content":"进入一个更有爱的领域",
                "type":"1",
                "comment":"69"
        }*/
    }
}
